<?php

include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Controllers.php';
include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ValidatorController.php';
include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'MyModules.php';
include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . 'lang_vi.php';

class MyControllers extends Controllers {

    function __construct($aryConfigDB, $aryConfigs) {
        parent::__construct();
        if ($this->getParam('ajax') != 1) {
//            $this->setHeader();
        }
        $this->_objConfig = $aryConfigs;
        $this->assign('root_page', $this->_rootPage);
        $this->_objModule = new MyModules($aryConfigDB);
    }

    function __destruct() {
        if ($this->getParam('ajax') != 1) {
//            $this->setFooter();
        }
    }

    public function def() {
        $this->setViewTemplate('customer', 'customer_register');
    }

    public function custommerRegister() {
        
        $aryData = $this->getAllPostParams();

        $objValidator = new Validator();

        $aryError = array();

        $objValidator->validateEmpty($aryData['customer_full_name'], _CUSTOMER_EMPTY_FULL_NAME);

        $objValidator->validateEmpty($aryData['customer_address'], _CUSTOMER_EMPTY_ADDRESS);

        $flag = $objValidator->validateEmpty($aryData['customer_phone_number'], _CUSTOMER_EMPTY_PHONE_NUMBER);
        
        if($flag == false){
            $flag = $objValidator->validatePhone($aryData['customer_phone_number'], _CUSTOMER_ERROR_PHONE_NUMBER);
        }

        $flag = $objValidator->validateEmpty($aryData['customer_date_check_in'], _CUSTOMER_EMPTY_DATE_CHECK_IN);

        if($flag == false){
            $flag = $objValidator->validateDateFormat($aryData['customer_date_check_in'], _CUSTOMER_ERROR_DATE_CHECK_IN);
        }

        $flag = $objValidator->validateEmpty($aryData['customer_date_check_out'], _CUSTOMER_EMPTY_DATE_CHECK_OUT);

        if($flag == false){
            $flag = $objValidator->validateDateFormat($aryData['customer_date_check_out'], _CUSTOMER_ERROR_DATE_CHECK_OUT);
        }

        $flag = $objValidator->validateEmpty($aryData['customer_number_of_room'], _CUSTOMER_EMPTY_NUMBER_OF_ROOM);
        if($flag == false){
            $flag = $objValidator->validateNumber($aryData['customer_number_of_room'], 'int', _CUSTOMER_ERROR_NUMBER_OF_ROOM);
        }

        $objValidator->validateEmpty($aryData['customer_room_type'], _CUSTOMER_EMPTY_ROOM_TYPE);

        $objValidator->validateEmpty($aryData['customer_bed_type'], _CUSTOMER_EMPTY_BED_TYPE);

        $objValidator->validateEmpty($aryData['customer_number_of_adults'], _CUSTOMER_EMPTY_NUMBER_OF_ADULTS);

        $objValidator->getResultAndErrors($aryError);

        if(count($aryError) > 0){
            $aryResult = array();
            $aryResult['intOk'] = -2;
            $aryResult['strError'] = join("\n", $aryError);
            echo json_encode($aryResult);
            return;
        }
        
        $aryCustomer = array();
        $aryCustomer['customer_full_name'] = $aryData['customer_full_name'];
        $aryCustomer['customer_gender'] = $aryData['customer_gender'];
        $aryCustomer['customer_nationality'] = $aryData['customer_nationality'];
        $aryCustomer['customer_address'] = $aryData['customer_address'];
        $aryCustomer['customer_phone_number'] = $aryData['customer_phone_number'];
        $aryCustomer['customer_email'] = $aryData['customer_email'];
        $aryCustomer['customer_date_check_in'] = str_replace("/", "-", $aryData['customer_date_check_in']);
        $aryCustomer['customer_date_check_out'] = str_replace("/", "-", $aryData['customer_date_check_out']);
        $aryCustomer['customer_number_of_room'] = $aryData['customer_number_of_room'];
        $aryCustomer['customer_room_type'] = $aryData['customer_room_type'];
        $aryCustomer['customer_bed_type'] = $aryData['customer_bed_type'];
        $aryCustomer['customer_number_of_adults'] = $aryData['customer_number_of_adults'];
        $aryCustomer['customer_number_of_children'] = $aryData['customer_number_of_children'];
        $aryCustomer['customer_note'] = $aryData['customer_note'];
        $aryCustomer['customer_create_date'] = date('Y-m-d: H:i:s');

        $intOk = $this->_objModule->insertCustomerRegister($aryCustomer);

        $aryResult = array();
        $aryResult['intOk'] = $intOk;
        $aryResult['message'] = _CUSTOMER_REGISTER_SUCCESS;
        echo json_encode($aryResult);
    }

}

?>
